﻿Public Class Damage_Chart
    Public Function GetDamage(ByVal attacker As ObjektEinheit, ByVal defender As ObjektEinheit, ByVal weapon As Integer) As Integer
        For I = 0 To RowIDS.Count - 1
            If RowIDS(I) = attacker.EinheitenID Then
                For i2 = 0 To ColIDS.Count - 1
                    If ColIDS(I) = attacker.EinheitenID Then
                        If weapon = 0 Then
                            Return Damages_weapon1(I)(i2)
                        Else
                            Return Damages_weapon2(I)(i2)
                        End If
                    End If
                Next
            End If
        Next
        Return -100
    End Function

    Shared damagae_charts As List(Of Damage_Chart)

    Dim RowIDS As List(Of String)
    Dim ColIDS As List(Of String)

    Dim Damages_weapon1 As List(Of List(Of Integer))
    Dim Damages_weapon2 As List(Of List(Of Integer))

    Public Sub New(ByVal File As String)
        RowIDS = New List(Of String)
        ColIDS = New List(Of String)
        Damages_weapon1 = New List(Of List(Of Integer))
        Damages_weapon2 = New List(Of List(Of Integer))
        'Open File
        Using Data As New IO.StreamReader(DataSettings.Resourcenpfad + "Damage Chart\" + File + ".html")
            'Read Data to End
            While Not Data.EndOfStream
                Dim line As String = Data.ReadLine.Trim.ToLower
                If line.StartsWith("<table frame") Then
                    Dim infos() As String = line.Split(" ")
                    Dim MaxCols As Integer = 0
                    For i = 0 To infos.Length - 1
                        If infos(i).StartsWith("cols") Then
                            MaxCols = CInt(infos(i).Split("=")(1))
                            Exit For
                        End If
                    Next
                    Dim Cols As Integer = 0
                    Dim ColIDsRead As Boolean = False
                    Dim NameInfo As Integer = 0
                    While Not Data.EndOfStream
                        line = Data.ReadLine.Trim.ToLower
                        If line.StartsWith("</tbody>") Then Exit While
                        'Info
                        If line.EndsWith("<br></td>") Then
                            If NameInfo = 2 Then
                                ColIDsRead = True
                            Else
                                NameInfo += 1
                            End If
                        ElseIf line.StartsWith("<td") And NameInfo = 2 Then
                            Dim info As String = line.Split(">")(1)
                            info = info.Remove(info.IndexOf("<"), info.Length - info.IndexOf("<"))
                            If ColIDsRead Then
                                ColIDS.Add(info.ToUpper)
                            ElseIf Cols = 0 Then 'Only Names
                                Cols += 1
                                Continue While
                            ElseIf Cols + 1 = MaxCols Then
                                If Not ColIDsRead Then
                                    RowIDS.Add(info.ToUpper)
                                End If
                            ElseIf Damages_weapon1.Count > 0 Then
                                Dim damage As String() = info.Split("/")
                                If IsNumeric(damage(0)) Then
                                    Damages_weapon1(Damages_weapon1.Count - 1).Add(damage(0))
                                Else
                                    Damages_weapon1(Damages_weapon1.Count - 1).Add(-1)
                                End If
                                If IsNumeric(damage(1)) Then
                                    Damages_weapon2(Damages_weapon2.Count - 1).Add(damage(1))
                                Else
                                    Damages_weapon2(Damages_weapon2.Count - 1).Add(-1)
                                End If
                                Cols += 1
                            End If
                        ElseIf line.StartsWith("</tr>") And Not ColIDsRead Then
                            Damages_weapon1.Add(New List(Of Integer))
                            Damages_weapon2.Add(New List(Of Integer))
                            Cols = 0
                        End If

                    End While
                End If
            End While
        End Using
        Damages_weapon1.RemoveAt(Damages_weapon1.Count - 1)
        Damages_weapon2.RemoveAt(Damages_weapon2.Count - 1)
    End Sub
End Class
